#include<iostream>
// #include"snake.h"
using namespace std;


#define max_size 10

class Snake{
private:
    int snake[max_size][max_size];
    int head_low;
    int head_upp;
    int tail_low;
    int tail_upp;
    int length;
public:
    Snake(int low = 0, int upp = 0, int len = 3);
    void moveHead(string direction);
    bool ifEatFood(int food_low, int food_upp);
    void eatFood();
    void addTail();
    void tailMove();
    bool isEnd(string nextDir);
    bool tailOut();
    void print();
};


//创建地图并移动尾巴
Snake::Snake(int low, int upp, int len):tail_low(low), tail_upp(upp), length(len){
    for(int i = 0;i < max_size;i++){
        for(int j = 0;j < max_size;j++){
            snake[i][j] = 0;
        }
    }
    for(int i = 0;i < length;i++){
        snake[low + i][upp] = i + 1;
    }
    head_low = tail_low + length - 1;
    head_upp = tail_upp;
}
//移动尾巴
void Snake::tailMove(){
    if(tail_upp < max_size && snake[tail_low][tail_upp + 1] == snake[tail_low][tail_upp] + 1){
        snake[tail_low][tail_upp] = 0;
        tail_upp += 1;
        return;
    }
    if(tail_low < max_size && snake[tail_low + 1][tail_upp] == snake[tail_low][tail_upp] + 1){
        snake[tail_low][tail_upp] = 0;
        tail_low += 1;
        return;
    }
    if(tail_low > 0 && snake[tail_low - 1][tail_upp] == snake[tail_low][tail_upp] + 1){
        snake[tail_low][tail_upp] = 0;
        tail_low -= 1;
        return;
    }
    if(tail_upp > 0 && snake[tail_low][tail_upp - 1] == snake[tail_low][tail_upp] + 1){
        snake[tail_low][tail_upp] = 0;
        tail_upp -= 1;
        return;
    }
}
//移动蛇头
void Snake::moveHead(string direction){
    

    if(direction == "a"){
        snake[head_low][head_upp - 1] = snake[head_low][head_upp] + 1;
        head_upp -= 1;
    }else if(direction == "s"){
        snake[head_low + 1][head_upp] = snake[head_low][head_upp] + 1;
        head_low += 1;
    }else if(direction == "d"){
        snake[head_low][head_upp + 1] = snake[head_low][head_upp] + 1;
        head_upp += 1;
    }else if(direction == "w"){
        snake[head_low - 1][head_upp] = snake[head_low][head_upp] + 1;
        head_low -= 1;
    }
}
//增长尾部
void Snake::addTail(){
    if(tail_upp < max_size && snake[tail_low][tail_upp + 1] == snake[tail_low][tail_upp] + 1){
        snake[tail_low][tail_upp - 1] = snake[tail_low][tail_upp] - 1;
        tail_upp -= 1;
        return;
    }
    if(tail_low < max_size && snake[tail_low + 1][tail_upp] == snake[tail_low][tail_upp] + 1){
        snake[tail_low - 1][tail_upp] = snake[tail_low][tail_upp] - 1;
        tail_low -= 1;
        return;
    }
    if(tail_low > 0 && snake[tail_low - 1][tail_upp] == snake[tail_low][tail_upp] + 1){
        snake[tail_low + 1][tail_upp] = snake[tail_low][tail_upp] - 1;
        tail_low += 1;
        return;
    }
    if(tail_upp > 0 && snake[tail_low][tail_upp - 1] == snake[tail_low][tail_upp] + 1){
        snake[tail_low][tail_upp + 1] = snake[tail_low][tail_upp] - 1;
        tail_upp += 1;
        return;
    }
}
//判断是否吃到食物
bool Snake::ifEatFood(int food_low, int food_upp){
    return snake[food_low][food_upp] > 0;
}
//吃到食物后尾部增长
void Snake::eatFood(){
    length ++;
    for(int i = 0;i < max_size;i++){
        for(int j = 0;j < max_size;j++){
            if(snake[i][j] > 0){
                snake[i][j] ++;
            }
        }
    }
    addTail();
}
//判断是否结束
bool Snake::isEnd(string nextDir){
    int upp = head_upp, low = head_low;
    if(nextDir == "a"){
        upp = head_upp - 1;
    }else if(nextDir == "s"){
        low = head_low + 1;
    }else if(nextDir == "d"){
         upp = head_upp + 1;
    }else if(nextDir == "w"){
        low = head_low - 1;
    }
    if(!(upp >=0 && upp < max_size)){
        return true;
    }
    if(!(low >= 0 && low < max_size)){
        return true;
    }
    return snake[low][upp] > 0;
}
//判断蛇尾是否超出地图
bool Snake::tailOut(){
    if(!(tail_upp >=0 && tail_upp < max_size)){
        return true;
    }
    if(!(tail_low >= 0 && tail_low < max_size)){
        return true;
    }
    return false;
}
//将地图跟蛇一起打印
void Snake::print(){
    for(int i = -1;i < max_size + 1;i++){
        cout << "#";
    }
    cout << endl;
    for(int i = 0;i < max_size;i++){
        cout << "#";
        for(int j = 0;j < max_size;j++){
            if(snake[i][j] > 0){
                if(i == head_low && j == head_upp){
                    cout << "H";
                }
                else if(i == tail_low && j == tail_upp){
                    cout << "T";
                }
                else{
                    cout << "*";
                }
            }
            else{
                cout << " ";
            }
        }
        cout << "#\n";
    }
    for(int i = -1;i < max_size + 1;i++){
        cout << "#";
    }
    cout << endl;
}


int main(){
    Snake t_snake;
    srand(time(0));
    // int food_low = rand() % max_size;
    // int food_upp = rand() % max_size;
    t_snake.print();//打印出初始蛇
    while(true){
        string direction;
        // cout << "方向键为‘2’-‘下’， ‘8’-‘上’， ’4‘-’左‘， ’6‘-’右‘" << endl;
        // cout << "各位大佬不要做一些奇奇怪怪的操作，程序不完善，跑出问题概不负责（比如不要往回走，故意走过墙什么的）" << endl;
        // cout << "方向";
        cin >> direction;
        if(t_snake.isEnd(direction)){
            cout << "碰到墙或者吃到自己了" << endl;
            break;
        }
        t_snake.moveHead(direction);
        t_snake.tailMove();
        if(t_snake.tailOut()){
            cout << "尾巴出去了" << endl;
            break;
        }
        // if(t_snake.ifEatFood(food_low, food_upp)){
        //     food_low = rand() % max_size;
        //     food_upp = rand() % max_size;
        //     t_snake.eatFood();
        // }
        t_snake.print();
    }
    cout << "Game Over" << endl;
    return 0;
}
